package com.tianchi.lexiang.background;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.HddDataBaseVersion;
import com.tianchi.lexiang.background.util.PublicUtil;
import com.tianchi.lexiang.background.util.SingerAddUtil;
import com.tianchi.lexiang.bean.LeXiangConstants;
import com.tianchi.lexiang.bean.Singer;
                                                                                    
public class UdiskAddSingers extends Activity implements OnClickListener {

	private static final String TAG = "SingerAdd";
	private PublicUtil publicUtil = PublicUtil.getPulibUtil();
	private SingerAddUtil instance = null;

	private TextView tipTxt;
	private TextView singerIntroduction;

	private ImageView singerPhoto;

	private EditText singerNameTxt;
	private EditText singerNameFirstSpellTxt;

	private ProgressBar singerNumsBar;

	private Spinner singerTypeSpinner;
	private Spinner singerAreaSpinner;

	private String singerTypeTxt;
	private String singerAreaTxt;

	private Button nextSingerBtn;
	private Button saveBtn;
	private Button backBtn;

	private int searchSingersNum;
	private String[] str;
	private String tipTitle;
	private int currentstar;

	private Singer singer;

	private ArrayAdapter<CharSequence> singerTypeAdapter;
	private ArrayAdapter<CharSequence> singerAreaAdapter;

	private HashMap<String, String> singerTypeMap = new HashMap<String, String>();
	
	private void setSingerTypeMap(){
		singerTypeMap.put(getString(R.string.male), "STP1");
		singerTypeMap.put(getString(R.string.female), "STP2");
		singerTypeMap.put(getString(R.string.combination), "STP3");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.singer_add);
		setSingerTypeMap();
		initView();
		
	}

	private void initView() {

		tipTxt = (TextView) findViewById(R.id.tip_txt);

		singerIntroduction = (TextView) findViewById(R.id.singer_introduction);

		singerPhoto = (ImageView) findViewById(R.id.singer_photo);

		singerNameTxt = (EditText) findViewById(R.id.singer_name_txt);

		singerNameFirstSpellTxt = (EditText) findViewById(R.id.singer_name_first_spell_txt);

		singerNumsBar = (ProgressBar) findViewById(R.id.singer_nums_bar);

		singerTypeAdapter = ArrayAdapter.createFromResource(this,
				R.array.singer_type, R.xml.spinner_item);
		singerTypeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		singerTypeSpinner = (Spinner) findViewById(R.id.singer_type_spinner);
		singerTypeSpinner.setAdapter(singerTypeAdapter);
		singerTypeSpinner
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						TextView tv = (TextView) view;
						tv.setTextSize(22.0f);
						singerTypeTxt = singerTypeAdapter.getItem(position)
								.toString();
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}
				});

		singerAreaAdapter = ArrayAdapter.createFromResource(this,
				R.array.singer_area, R.xml.spinner_item);
		singerAreaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		singerAreaSpinner = (Spinner) findViewById(R.id.singer_area_spinner);
		singerAreaSpinner.setAdapter(singerAreaAdapter);
		singerAreaSpinner
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						TextView tv = (TextView) view;
						tv.setTextSize(22.0f);
						singerAreaTxt = singerAreaAdapter.getItem(position)
								.toString();
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}

				});
		nextSingerBtn = (Button) findViewById(R.id.next_btn);
		nextSingerBtn.setOnClickListener(this);
		saveBtn = (Button) findViewById(R.id.save_btn);
		saveBtn.setOnClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(this);
		
		instance = SingerAddUtil.getInstece(this);
		String singerNames = instance.readAddSinersName();
		if (singerNames != null && !"".equals(singerNames)) {
			str = singerNames.split(",");
			searchSingersNum = str.length;
			currentstar = 1;
			tipTitle = getString(R.string.tip_title_one) + searchSingersNum
					+ getString(R.string.tip_title_two) + currentstar
					+ getString(R.string.tip_title_three);
		} else {
			tipTitle = getString(R.string.tip_title_non);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		singerNumsBar.setMax(searchSingersNum);
		nextSinger();
	}

	private void nextSinger() {
		singerNumsBar.setProgress(currentstar);
		tipTxt.setText(tipTitle);
		singerPhoto.setImageDrawable(Drawable.createFromPath(LeXiangConstants.SINGER_UDISK_PATH+ str[currentstar - 1] + ".jpg"));
		singerNameTxt.setText(str[currentstar - 1]);
		singerNameFirstSpellTxt.setText(publicUtil.getConvertStr(str[currentstar - 1]));
		singerIntroduction.setText(instance.convertCodeAndGetText( LeXiangConstants.SINGER_UDISK_PATH + str[currentstar - 1]));
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.next_btn:
			currentstar++;
			Log.d(TAG, "currentstar===>" + currentstar);
			if (currentstar <= searchSingersNum) {
				tipTitle = getString(R.string.tip_title_one) + searchSingersNum
						+ getString(R.string.tip_title_two) + currentstar
						+ getString(R.string.tip_title_three);
				nextSinger();
			}
			break;
		case R.id.save_btn:
			singer = new Singer();
			Log.d(TAG, "singerTypeTxt===>" + singerTypeTxt);
			if (!getString(R.string.please_choose).equals(singerTypeTxt)
					&& !getString(R.string.please_choose).equals(singerAreaTxt)) {
				singer.name = singerNameTxt.getText().toString();
				singer.type = singerTypeMap.get(singerTypeTxt);
				singer.area = singerAreaTxt;
				singer.firstSpell = singerNameFirstSpellTxt.getText()
						.toString();
				singer.information = singerIntroduction.getText().toString();
				boolean bool = publicUtil.copyInfo(LeXiangConstants.SINGER_UDISK_PATH
						+ singer.name + ".jpg", LeXiangConstants.SIGER_HARD_DISK_PATH
						+ singer.name + ".jpg");
				if (instance.insertSingerInfo(singer) == 0 && bool) {
					currentstar++;
					if (currentstar <= searchSingersNum) {
						tipTitle = getString(R.string.tip_title_one)
								+ searchSingersNum
								+ getString(R.string.tip_title_two)
								+ currentstar
								+ getString(R.string.tip_title_three);
						nextSinger();
					}
				} else if (instance.insertSingerInfo(singer) == 2) {
					dialog();
				}
			} else {
				errorDialog();
			}
			break;
		case R.id.back_btn:
//			DatabaseVersionUtil.updateDBVersion(SingerAdd.this);
			HddDataBaseVersion.setCurrentHddDataBaseVersion();
			UdiskAddSingers.this.finish();
			break;

		default:
			break;
		}
	}

	private void errorDialog() {
		AlertDialog.Builder builder = new Builder(UdiskAddSingers.this);
		builder.setMessage(getString(R.string.please_input_singer_info_complete));

		builder.setTitle(getString(R.string.tip));

		builder.setPositiveButton(getString(R.string.confirm), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();
	}

	private void dialog() {
		AlertDialog.Builder builder = new Builder(UdiskAddSingers.this);
		builder.setMessage(getString(R.string.singer_has_existed));
		builder.setTitle(getString(R.string.tip));
		builder.setPositiveButton(getString(R.string.yes), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				if (instance.updateSingerInfo(singer)) {
					currentstar++;
					if (currentstar <= searchSingersNum) {
						tipTitle = getString(R.string.tip_title_one)
								+ searchSingersNum
								+ getString(R.string.tip_title_two)
								+ currentstar
								+ getString(R.string.tip_title_three);
						nextSinger();
					}
				}
			}
		});

		builder.setNegativeButton(getString(R.string.no), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				currentstar++;
				if (currentstar <= searchSingersNum) {
					tipTitle = getString(R.string.tip_title_one)
							+ searchSingersNum
							+ getString(R.string.tip_title_two) + currentstar
							+ getString(R.string.tip_title_three);
					nextSinger();
				}
			}
		});
		builder.create().show();
	}
}
